program test_02_soar;

uses SysUtils, Gl, Glu, Glut, OpenGls, Debugs, Keyboards, Fonts, FpsCounters, Grounds, Cameras, Vectors, VUMaths;

var 
  Frame : integer;

procedure OnDisplay; cdecl;
begin
  // OpenGL soap
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLoadIdentity;
  
  // keyboard pan
  if KeyIsDown(vkUp) then    Ground.Direction := Vec3RotateY(Ground.Direction,+3);
  if KeyIsDown(vkDown) then  Ground.Direction := Vec3RotateY(Ground.Direction,-3);
  if KeyIsDown(vkLeft) then  Ground.Direction := Vec3RotateZ(Ground.Direction,+3);
  if KeyIsDown(vkRight) then Ground.Direction := Vec3RotateZ(Ground.Direction,-3);
  // movement
  if KeyIsDown(vkSmallA) then Ground.Position := Vec3Add(Ground.Position,Ground.Direction);
  // exit
  if KeyIsDown(vkEsc) then halt;
  // debug position and direction
  if KeyIsDown(vkF10) then
    writeln('Ground: Position=',Vector3ToStr(Ground.Position),' Direction=',Vector3ToStr(Ground.Direction));

  // time
  frame := frame + 30;
  
  // ground
  Ground.Draw;
  
  // model

  // print FPS
  Fps.Tic;
  Font.OutTextXY(10,10,'FPS: '+inttostr(Fps.Effective));
  
  // refresh
  glutSwapBuffers;
end;

procedure OnIdle; cdecl;
begin
  glutPostRedisplay;
end;

begin
  InitOpenGl;
  Debug.Enable(dlInput);
  
  Font := TFont.Create('data/font/dejavu_mono.png');
  Fps.Limit := 1000;

  // ground
  if ParamStr(1) <> '' then
    Ground.Initialize('data/',ParamStr(1))
  else
    Ground.Initialize('data/','dunes');

  // initial position and direction
  Ground.Position := Vector3(80.26, 496.94, 163.78);
  Ground.Direction := Vector3(1.27, -0.09, -0.23);

  // callbacks
  glutDisplayFunc(@OnDisplay);
  glutIdleFunc(@OnIdle);

  // main loop
  glutMainLoop;
end.
